﻿#region

using System.Windows;
using System.Windows.Controls;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.Workflow;
using NetBay.Core.BusinessEntities.Workflow.Rules;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;

#endregion

namespace Rio.Framework.Pages.Workflow
{
    public class WorkflowActionsColumn : GridViewBoundColumnBase
    {
        #region Readonly & Static Fields

        public static readonly DependencyProperty CurrentFolderProperty =
            DependencyProperty.Register("CurrentFolder", typeof (Folder), typeof (WorkflowActionsColumn), null);

        public static readonly DependencyProperty CurrentFolderTypeProperty =
            DependencyProperty.Register("CurrentFolderType", typeof (FolderType), typeof (WorkflowActionsColumn), null);

        public static readonly DependencyProperty CurrentOrganizationProperty =
            DependencyProperty.Register("CurrentOrganization", typeof (Organization), typeof (WorkflowActionsColumn), null);

        public static readonly DependencyProperty WorkflowInstanceProperty =
            DependencyProperty.Register("WorkflowInstance", typeof (WorkflowInstance), typeof (WorkflowActionsColumn), null);

        public static readonly DependencyProperty WorkflowRulesProperty =
            DependencyProperty.Register("WorkflowRules", typeof (Rules), typeof (WorkflowActionsColumn), null);

        #endregion

        #region Instance Properties

        public Folder CurrentFolder
        {
            get
            {
                return (Folder) GetValue(CurrentFolderProperty);
            }
            set
            {
                SetValue(CurrentFolderProperty, value);
            }
        }

        public FolderType CurrentFolderType
        {
            get
            {
                return (FolderType) GetValue(CurrentFolderTypeProperty);
            }
            set
            {
                SetValue(CurrentFolderTypeProperty, value);
            }
        }

        public Organization CurrentOrganization
        {
            get
            {
                return (Organization) GetValue(CurrentOrganizationProperty);
            }
            set
            {
                SetValue(CurrentOrganizationProperty, value);
            }
        }

        public WorkflowActionsColumnType Type { get; set; }

        public WorkflowInstance WorkflowInstance
        {
            get
            {
                return (WorkflowInstance) GetValue(WorkflowInstanceProperty);
            }
            set
            {
                SetValue(WorkflowInstanceProperty, value);
            }
        }

        public Rules WorkflowRules
        {
            get
            {
                return (Rules) GetValue(WorkflowRulesProperty);
            }
            set
            {
                SetValue(WorkflowRulesProperty, value);
            }
        }

        #endregion

        #region Instance Methods

        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            StackPanel st = new StackPanel();
            st.Orientation = Orientation.Horizontal;

            //if (WorkflowRules.Conditions != null
            //    && WorkflowRules.Conditions.Any(i => i.Id == WorkflowInstance.StartingCondition))
            //{
            //    Condition startingCondition = WorkflowRules.Conditions.Where(i => i.Id == WorkflowInstance.StartingCondition).SingleOrDefault();

            //    var sortedActions = startingCondition.Actions.Where(i => WorkflowInstance.Definition.Actions.Select(j => j.ActionOrConditionId).Contains(i.Id)).OrderBy(i => i.UiTypeAction);

            //    foreach (var action in sortedActions)
            //    {
            //        WorkflowButtonActionBase workflowButtonActionBase = null;

            //        switch (action.UiTypeAction)
            //        {
            //            case UiTypeAction.Index:
            //                workflowButtonActionBase = new WorkflowIndexButtonAction()
            //                {
            //                    Margin = new Thickness(3),
            //                    DataContext = new WorkflowButtonActionDataContext()
            //                    {
            //                        WorkflowInstance = WorkflowInstance,
            //                        WorkflowRules = WorkflowRules,
            //                        UiTypeAction = WorkflowRulesUiTypeAction.Index,
            //                        Organization = CurrentOrganization,
            //                        FolderType = CurrentFolderType,
            //                        Folder = CurrentFolder,
            //                        ActionParameters = action.Parameters
            //                    }
            //                };
            //                break;

            //            case UiTypeAction.Forward:
            //                workflowButtonActionBase = new WorkflowForwardButtonAction()
            //                {
            //                    Margin = new Thickness(3),
            //                    DataContext = new WorkflowButtonActionDataContext()
            //                    {
            //                        WorkflowInstance = WorkflowInstance,
            //                        WorkflowRules = WorkflowRules,
            //                        UiTypeAction = UiTypeAction.Forward,
            //                        Organization = CurrentOrganization,
            //                        FolderType = CurrentFolderType,
            //                        Folder = CurrentFolder,
            //                        ActionParameters = action.Parameters
            //                    }
            //                };
            //                break;

            //            case UiTypeAction.Reply:
            //                workflowButtonActionBase = new WorkflowReplyButtonAction()
            //                {
            //                    Margin = new Thickness(3),
            //                    DataContext = new WorkflowButtonActionDataContext()
            //                    {
            //                        WorkflowInstance = WorkflowInstance,
            //                        WorkflowRules = WorkflowRules,
            //                        UiTypeAction = UiTypeAction.Reply,
            //                        Organization = CurrentOrganization,
            //                        FolderType = CurrentFolderType,
            //                        Folder = CurrentFolder,
            //                        ActionParameters = action.Parameters
            //                    }
            //                };
            //                break;
            //            case UiTypeAction.Read:
            //                workflowButtonActionBase = new WorkflowReadButtonAction()
            //                {
            //                    Margin = new Thickness(3),
            //                    DataContext = new WorkflowButtonActionDataContext()
            //                    {
            //                        WorkflowInstance = WorkflowInstance,
            //                        WorkflowRules = WorkflowRules,
            //                        UiTypeAction = UiTypeAction.Read,
            //                        Organization = CurrentOrganization,
            //                        FolderType = CurrentFolderType,
            //                        Folder = CurrentFolder,
            //                        ActionParameters = action.Parameters
            //                    }
            //                };
            //                break;
            //            case UiTypeAction.Validate:
            //                workflowButtonActionBase = new WorkflowValidatedButtonAction()
            //                {
            //                    Margin = new Thickness(3),
            //                    DataContext = new WorkflowButtonActionDataContext()
            //                    {
            //                        WorkflowInstance = WorkflowInstance,
            //                        WorkflowRules = WorkflowRules,
            //                        UiTypeAction = UiTypeAction.Validate,
            //                        Organization = CurrentOrganization,
            //                        FolderType = CurrentFolderType,
            //                        Folder = CurrentFolder,
            //                        ActionParameters = action.Parameters
            //                    }
            //                };
            //                break;

            //            case UiTypeAction.Refuse:
            //                workflowButtonActionBase = new WorkflowRefusedButtonAction()
            //                {
            //                    Margin = new Thickness(3),
            //                    DataContext = new WorkflowButtonActionDataContext()
            //                    {
            //                        WorkflowInstance = WorkflowInstance,
            //                        WorkflowRules = WorkflowRules,
            //                        UiTypeAction = UiTypeAction.Refuse,
            //                        Organization = CurrentOrganization,
            //                        FolderType = CurrentFolderType,
            //                        Folder = CurrentFolder,
            //                        ActionParameters = action.Parameters
            //                    }
            //                };
            //                break;
            //            case UiTypeAction.Insert:
            //                workflowButtonActionBase = new WorkflowInsertButtonAction()
            //                {
            //                    Margin = new Thickness(3),
            //                    DataContext = new WorkflowButtonActionDataContext()
            //                    {
            //                        WorkflowInstance = WorkflowInstance,
            //                        WorkflowRules = WorkflowRules,
            //                        UiTypeAction = UiTypeAction.Insert,
            //                        Organization = CurrentOrganization,
            //                        FolderType = CurrentFolderType,
            //                        Folder = CurrentFolder,
            //                        ActionParameters = action.Parameters
            //                    }
            //                };
            //                break;
            //            case UiTypeAction.Cancel:
            //                workflowButtonActionBase = new WorkflowRefusedButtonAction()
            //                {
            //                    Margin = new Thickness(3),
            //                    DataContext = new WorkflowButtonActionDataContext()
            //                    {
            //                        WorkflowInstance = WorkflowInstance,
            //                        WorkflowRules = WorkflowRules,
            //                        UiTypeAction = UiTypeAction.Cancel,
            //                        Organization = CurrentOrganization,
            //                        FolderType = CurrentFolderType,
            //                        Folder = CurrentFolder,
            //                        ActionParameters = action.Parameters
            //                    }
            //                };
            //                break;
            //            default:
            //                throw new NotImplementedException();
            //                break;
            //        }

            //        if (Type == WorkflowActionsColumnType.Document)
            //        {
            //            workflowButtonActionBase.DataContext.Document = dataItem as WorkflowDocument;
            //        }

            //        st.Children.Add(workflowButtonActionBase);
            //    }
            //}

            return st;
        }

        #endregion
    }

    public enum WorkflowActionsColumnType
    {
        Document,
        Global
    }
}